package com.xkcyy.one.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xkcyy.one.entity.Agent;
import com.xkcyy.one.exception.BusinessException;
import com.xkcyy.one.mapper.AgentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 智能体服务类
 */
@Service
public class AgentService {
    
    private final AgentMapper agentMapper;
    
    @Autowired
    public AgentService(AgentMapper agentMapper) {
        this.agentMapper = agentMapper;
    }
    
    /**
     * 获取所有智能体
     *
     * @return 智能体列表
     */
    public List<Agent> getAllAgents() {
        return agentMapper.selectList(null);
    }
    
    /**
     * 根据状态获取智能体列表
     *
     * @param status 状态
     * @return 智能体列表
     */
    public List<Agent> getAgentsByStatus(String status) {
        LambdaQueryWrapper<Agent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Agent::getStatus, status);
        return agentMapper.selectList(queryWrapper);
    }
    
    /**
     * 根据ID获取智能体
     *
     * @param id 智能体ID
     * @return 智能体
     */
    public Agent getAgentById(Long id) {
        Agent agent = agentMapper.selectById(id);
        if (agent == null) {
            throw new BusinessException("智能体不存在，ID: " + id);
        }
        return agent;
    }
    
    /**
     * 创建智能体
     *
     * @param agent 智能体
     * @return 创建后的智能体
     */
    public Agent createAgent(Agent agent) {
        // 检查名称是否已存在
        LambdaQueryWrapper<Agent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Agent::getName, agent.getName());
        if (agentMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("智能体名称已存在: " + agent.getName());
        }
        
        // 设置默认值
        agent.setVersion(1);
        agent.setStatus("ACTIVE");
        if (agent.getTrainable() == null) {
            agent.setTrainable(true);
        }
        
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        agent.setCreateTime(now);
        agent.setUpdateTime(now);
        
        // 插入智能体
        agentMapper.insert(agent);
        
        return agent;
    }
    
    /**
     * 更新智能体
     *
     * @param agent 智能体
     * @return 更新后的智能体
     */
    public Agent updateAgent(Agent agent) {
        // 检查智能体是否存在
        Agent existingAgent = agentMapper.selectById(agent.getId());
        if (existingAgent == null) {
            throw new BusinessException("智能体不存在，ID: " + agent.getId());
        }
        
        // 检查名称是否已被其他智能体使用
        LambdaQueryWrapper<Agent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Agent::getName, agent.getName())
                .ne(Agent::getId, agent.getId());
        if (agentMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("智能体名称已存在: " + agent.getName());
        }
        
        // 保持版本号不变
        agent.setVersion(existingAgent.getVersion());
        
        // 设置更新时间
        agent.setUpdateTime(LocalDateTime.now());
        
        // 更新智能体
        agentMapper.updateById(agent);
        
        return agent;
    }
    
    /**
     * 删除智能体
     *
     * @param id 智能体ID
     */
    public void deleteAgent(Long id) {
        // 检查智能体是否存在
        Agent existingAgent = agentMapper.selectById(id);
        if (existingAgent == null) {
            throw new BusinessException("智能体不存在，ID: " + id);
        }
        
        // 删除智能体
        agentMapper.deleteById(id);
    }
    
    /**
     * 更新智能体状态
     *
     * @param id     智能体ID
     * @param status 新状态
     * @return 更新后的智能体
     */
    public Agent updateAgentStatus(Long id, String status) {
        Agent agent = agentMapper.selectById(id);
        if (agent == null) {
            throw new BusinessException("智能体不存在，ID: " + id);
        }
        
        agent.setStatus(status);
        agent.setUpdateTime(LocalDateTime.now());
        agentMapper.updateById(agent);
        
        return agent;
    }
    
    /**
     * 更新智能体系统提示词
     *
     * @param id           智能体ID
     * @param systemPrompt 新系统提示词
     * @return 更新后的智能体
     */
    public Agent updateAgentSystemPrompt(Long id, String systemPrompt) {
        Agent agent = agentMapper.selectById(id);
        if (agent == null) {
            throw new BusinessException("智能体不存在，ID: " + id);
        }
        
        agent.setSystemPrompt(systemPrompt);
        agent.setVersion(agent.getVersion() + 1);
        agent.setUpdateTime(LocalDateTime.now());
        agentMapper.updateById(agent);
        
        return agent;
    }
} 